package com.data4.supplier.controller;


import com.data4.feignapi.common.constants.GatewayConstants;
import com.data4.feignapi.dto.CategoryDTO;
import com.data4.feignapi.dto.CategoryUpdateDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.dto.StandardsUpdateDTO;
import com.data4.supplier.service.CategoryService;
import com.data4.supplier.service.StandardsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/category")
@Tag(name = "商品分类管理")
public class CategoryController {
    /**
     * 由于分类绑定规格，故而对于规格的特殊修改，在这个接口下操作实现。
     */
    @Resource
    private CategoryService categoryService;
    @Resource
    private StandardsService standardsService;

    @PostMapping("/addCategoryAndStandard")
    @Operation(summary = "添加分类")
    public Result addCategory(@RequestBody CategoryDTO categoryAndStandard,
                              @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return categoryService.addCategory(categoryAndStandard,Integer.parseInt(supplierId));
    }

    @PutMapping("/updateCategory/{categoryId}")
    @Operation(summary = "修改分类和规格")
    public Result updateCategory(@RequestBody CategoryUpdateDTO category,
                                 @PathVariable Integer categoryId,
                                 @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        System.err.println(category);
        return categoryService.updateCategory(category, categoryId, Integer.parseInt(supplierId));
    }

    //删除分类必须要删除其绑定的规格
    @DeleteMapping("/delete")
    @Operation(summary = "删除分类")
    public Result delete(@RequestBody List<Integer> categoryIdList,
                         @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return categoryService.deleteCategory(categoryIdList, Integer.parseInt(supplierId));
    }

    @GetMapping("/getCategory/{current}/{num}")
    @Operation(summary = "根据时间查询分类")
    public Result getCategory(@PathVariable Integer current, @PathVariable Integer num,
                              @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId) {
        return categoryService.getCategory(current, num, Integer.parseInt(supplierId));
    }

    @GetMapping("/getCategoryByName/{current}/{num}")
    @Operation(summary = "模糊查询分类", description = "输入的只要还有即可查")
    public Result getCategoryByName(@PathVariable Integer current,
                                    @PathVariable Integer num,
                                    @RequestParam("ParentName") String ParentName,
                                    @RequestParam(value = "childName", required = false)
                                    String childName,
                                    @RequestHeader(GatewayConstants.X_SUPPLIER_ID) String supplierId
    ) {
        return categoryService.getCategoryByNameLike(ParentName, childName, current,
                num, Integer.parseInt(supplierId));
    }

    @GetMapping("/getStandards/{standardsId}")
    @Operation(summary = "获取规格")
    public Result getStandards(@PathVariable Integer standardsId){
        return standardsService.getStandards(standardsId);
    }
}

